package org.springblade.feign;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.AllArgsConstructor;
import org.springblade.core.tool.api.R;
import org.springblade.dto.ProtocolAnalysisDTO;
import org.springblade.entity.Product;
import org.springblade.service.IProductService;
import org.springblade.service.IProtocolAnalysisService;
import org.springblade.vo.ProductVO;
import org.springblade.vo.ProtocolAnalysisVO;
import org.springblade.wrapper.ProductWrapper;
import org.springblade.wrapper.ProtocolAnalysisWrapper;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@AllArgsConstructor
public class DeviceManagerClient implements IDeviceManagerClient {
	@Resource
	private IProtocolAnalysisService protocolAnalysisService;

	@Resource
	private IProductService productService;

	@Override
	public List<ProtocolAnalysisVO> getControlList(ProtocolAnalysisDTO protocolAnalysisDTO) {
		return protocolAnalysisService.getControlList(protocolAnalysisDTO);
	}

	@Override
	public R<Boolean> sentControlCmd(ProtocolAnalysisDTO protocolAnalysisDTO) {
		return protocolAnalysisService.sendCode(String.valueOf(protocolAnalysisDTO.getId()));
	}

	/**
	 * 获取协议列表
	 * @param address
	 * @param port
	 * @return
	 */
	@Override
	public List<ProtocolAnalysisVO> getProtocolAnalysisList(String address, String port, Integer direction) {
		return protocolAnalysisService.getProtocolAnalysisList(address, port, direction);
	}

	/**
	 * 更新协议数据
	 * @param protocolAnalysisVO
	 */
	@Override
	public void updateProtocolData(ProtocolAnalysisVO protocolAnalysisVO) {
		protocolAnalysisService.updateById(protocolAnalysisVO);
	}

	/**
	 * 获取解码getOne
	 * @param protocolAnalysisId
	 * @return
	 */
	@Override
	public ProtocolAnalysisVO getProtocolAnalysisOne(Long protocolAnalysisId) {
		return ProtocolAnalysisWrapper.build().entityVO(protocolAnalysisService.getById(protocolAnalysisId));
	}

	/**
	 * 产品查询GetOne
	 * @param address
	 * @param port
	 * @return
	 */
	@Override
	public ProductVO getProductOne(String address, String port) {
		Product product = productService.getOne(Wrappers.<Product>lambdaQuery().eq(Product::getIpAddr, address).eq(Product::getPort, port));
		return ProductWrapper.build().entityVO(product);
	}

	/**
	 * initConnect
	 * @return
	 */
	@Override
	public List<ProductVO> initConnect() {
		return ProductWrapper.build().listVO(productService.list(Wrappers.<Product>lambdaQuery().eq(Product::getIsDeleted, 0)));
	}

	/**
	 * updateProtocolStatus
	 * @param product
	 */
	@Override
	public void updateProtocolStatus(Product product) {
		productService.upOrDownSocket(product);
	}

	/**
	 * 测试产品连通性
	 */
	@Override
	public void testIpAndPort() {
		productService.testIpAndPort("");
	}

}
